//*******************************************************************
//
//  Init     Date      Tag       Description
//  -----   -------   -----     -------------
//   DSB    7/01/12   PRO_BAR    Creates and instantiates progress bar
//
//   DSB    7/01/12   LOAD_BT    Implements Load Event for new UML
//
//   DSB    7/01/12   CRE_UML    Implements Creating a new UML
//
//   DSB    7/04/12   MEN_BAR    Implements the Menu Bar of the GUI
//*******************************************************************
package umlteam.src.main.java.edu.umuc.duchon.umlteam.main.gui;

import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JProgressBar;
import javax.swing.JButton;
import javax.swing.JList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import umlteam.src.main.java.edu.umuc.duchon.umlteam.main.parsers.Parser;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class main {

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					main window = new main();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public main() {
		initialize();
	}

	private void initialize() {

		frame = new JFrame("UML Creator");
		frame.setBounds(100, 100, 500, 300);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

		// ********** PRO_BAR *** Start **************************************

		progressBar.setBounds(338, 217, 146, 19);
		frame.getContentPane().add(progressBar);

		// ********** PRO_BAR *** Stop ***************************************

		// ********** CRE_UML *** Start **************************************

		createUmlButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				sendParse();
				displayUML();
			}
		});
		createUmlButton.setBounds(10, 164, 125, 23);
		frame.getContentPane().add(createUmlButton);
		createUmlButton.setEnabled(false);

		// ********** CRE_UML *** Stop ***************************************

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 392, 143);
		frame.getContentPane().add(scrollPane);

		scrollPane.setViewportView(list);

		// ********** LOAD_BT *** Start **************************************

		load.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					fileSelectPerformLoad();
				} catch (Exception e) {
					JOptionPane.showMessageDialog(frame, "No Files Found");
				}
			}
		});
		load.setBounds(10, 213, 125, 23);
		frame.getContentPane().add(load);

		// ********** LOAD_BT *** Stop ***************************************

		JButton openUML = new JButton("Open UML");
		openUML.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openUML();
			}
		});

		openUML.setBounds(145, 213, 125, 23);
		frame.getContentPane().add(openUML);

		// ********** MEN_BAR *** Start **************************************

		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);

		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);

		JMenuItem mntmFillIn = new JMenuItem("New Project");
		mntmFillIn.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent arg0) {
				newProject();
			}
		});
		mnFile.add(mntmFillIn);

		JMenuItem mntmLoadJavaFiles = new JMenuItem("Load Java Files");
		mntmLoadJavaFiles.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent arg0) {
				fileSelectPerformLoad();
			}
		});
		mnFile.add(mntmLoadJavaFiles);

		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent arg0) {
				System.exit(0);
			}
		});
		mnFile.add(mntmExit);

		JMenu mnEdit = new JMenu("Edit");
		menuBar.add(mnEdit);

		JMenuItem mntmFillIn_1 = new JMenuItem("FILL IN");
		mnEdit.add(mntmFillIn_1);

		// ********** MEN_BAR *** Stop ***************************************
	}

	/** DECLERATIONS **/
	final JList list = new JList();
	final ArrayList<String> javaFiles = new ArrayList<String>();
	final JButton createUmlButton = new JButton("Create UML");
	final JButton load = new JButton("Load Java Files");
	final JFileChooser chooser = new JFileChooser();
	final JProgressBar progressBar = new JProgressBar();
	final ArrayList parsedArray = new ArrayList();

	private JFrame frame;
	private File[] fileArray;

	/** PRIVATE CLASS METHODS **/

	// Loads a new project if the user selects yes
	// Gives the option to load source directly after selecting yes.
	private void newProject() {

		int dialogButton = JOptionPane.YES_NO_OPTION;
		// Clears out project elements
		int dialogResult = JOptionPane.showConfirmDialog(null,
				"Would you like to start a new project?", "Warning",
				dialogButton);

		if (dialogResult == JOptionPane.YES_OPTION) {
			list.setListData(javaFiles.toArray());
			createUmlButton.setEnabled(false);

			// Perform Load procedure
			dialogResult = JOptionPane.showConfirmDialog(null,
					"Would you like to load the source folder?", "New Project",
					dialogButton);

			if (dialogResult == JOptionPane.YES_OPTION) {
				fileSelectPerformLoad();
			}
		}
	}

	// TODO add OPEN_UML functionality
	private void openUML() {

		JOptionPane.showMessageDialog(frame,
				"Unimplemented feature. Future release.");

	}

	@SuppressWarnings("unchecked")
	private void sendParse() {

		progressBar.setIndeterminate(true);


		Object[] tempList = list.getSelectedValuesList().toArray();
		fileArray = new File[tempList.length];
		Parser parser1;

		try{
			for(int i = 0; i < tempList.length; i++)
				fileArray[i] = new File(tempList[i].toString());
			
			for(int i = 0; i < fileArray.length; i++){
				parser1 = new Parser(fileArray[i]);
				parser1.parseFile();
				parsedArray.add(parser1);

			}
		}catch(Exception e){
			JOptionPane.showMessageDialog(frame,
					"Error in file.");
		}
		progressBar.setIndeterminate(false);

	}

	private void displayUML(){
		
		Object[] temp;
		temp = parsedArray.toArray();
		for(int i = 0; i < parsedArray.size(); i++)
			System.out.println(temp[i]);

		
	}
	
	
	
	// Opens a JFile Chooser and allows user to select a source folder
	// to be associated with the project.
	private void fileSelectPerformLoad() {

		int returnVal = chooser.showOpenDialog(chooser);

		if (returnVal == JFileChooser.APPROVE_OPTION) {

			javaFiles.clear();
			stepThroughDirectories(chooser.getSelectedFile().getAbsolutePath());
			list.setListData(javaFiles.toArray());
			createUmlButton.setEnabled(true);

		}
	}

	// ********** REC_DIR *** Start **************************************
	private void stepThroughDirectories(String path) {

		File location = new File(path);
		File[] list = location.listFiles();

		for (File f : list) {
			if (f.isDirectory()) {
				stepThroughDirectories(f.getAbsolutePath());
			} else {
				if (f.getName().endsWith(".java")
						|| f.getName().endsWith(".Java"))
					javaFiles.add(f.getAbsolutePath());
			}
		}

	}
	// ********** REC_DIR *** Stop ***************************************
}
